package com.im.partchat.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.im.partchat.R;
import com.im.partchat.bean.Group;

import java.util.List;

/**
 * Created by hhz on 2015/6/17.
 */
public class GroupListAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater inflater;
    private List<Group> groupInfoBeans;

    public GroupListAdapter(Context context, List<Group> groupInfoBeans) {
        this.mContext = context;
        inflater = LayoutInflater.from(mContext);
        this.groupInfoBeans = groupInfoBeans;
    }


    @Override
    public int getCount() {
        if (groupInfoBeans != null) return groupInfoBeans.size();
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (groupInfoBeans != null) return groupInfoBeans.get(position);
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        Group ginfoBean = groupInfoBeans.get(position);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.grouplist_item, null);
            viewHolder = new ViewHolder(ginfoBean);
            viewHolder.groupName = (TextView) convertView.findViewById(R.id.tv_name);
            convertView.setTag(viewHolder);
            viewHolder.refresh();
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
            viewHolder.update(ginfoBean);
        }
        return convertView;
    }

    public class ViewHolder {
        TextView groupName;

        private Group groupInfoBean;

        public ViewHolder(Group groupInfoBean) {
            this.groupInfoBean = groupInfoBean;
        }


        public void update(Group groupInfoBean) {
            this.groupInfoBean = groupInfoBean;
            refresh();
        }


        public void refresh() {
            groupName.setText(groupInfoBean.getName());
        }


    }


}
